package cn.hl.hm.controller.driver;

import cn.hl.hm.logic.driver.DriverInfoLogic;
import cn.hl.hm.util.InfoCode;
import cn.hl.hm.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLDataException;

/**
 * 司机和车辆
 * @author tarzan
 * @date 20171024 15:52
 */
@Controller
@RequestMapping("/driver")
public class DriverInfoController {
    private static final Logger logger = LoggerFactory.getLogger(DriverInfoController.class);
    @Autowired
    private DriverInfoLogic driverService;

    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public String info(@CookieValue(required = false,value = "driverToken") String driverToken,
                        @RequestHeader(required = false) String deviceToken,
                        HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("司机---基本信息---driver/info");
        request.setCharacterEncoding("UTF-8");
        try {
            Result.ok(driverService.getDriverInfo(driverToken,deviceToken), InfoCode.SUCCESS, response);
        } catch (Exception e) {
            e.printStackTrace();
            Result.ok(null, InfoCode.FAILURE, response);
        }
        return null;
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public String detail(@CookieValue(required = false,value = "driverToken") String driverToken,
                        @RequestHeader(required = false) String deviceToken,
                        HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("司机---基本信息---driver/detail");
        request.setCharacterEncoding("UTF-8");
        try {
            Result.ok(driverService.getDriverDetail(driverToken,deviceToken), InfoCode.SUCCESS, response);
        } catch (Exception e) {
            e.printStackTrace();
            Result.ok(null, InfoCode.FAILURE, response);
        }
        return null;
    }

    @RequestMapping(value = "vehicle/add", method = RequestMethod.POST)
    public String addVehicle(@CookieValue(required = false,value = "driverToken") String driverToken,
                        @RequestParam(required = true) String plateNumber,
                        @RequestParam(required = false) Integer vehicleTpyeId,
                        @RequestParam(required = false) Integer vehicleLengthId,
                        @RequestParam(required = false) String transportLicense,
                        @RequestParam(required = false) String drivingLicensePic,
                        @RequestParam(required = false) String drivingLicenseGuaPic,
                        HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("司机---添加车辆---vehicle/add");
        request.setCharacterEncoding("UTF-8");
        try {
            Boolean state= driverService.addVehicle(driverToken, plateNumber, vehicleTpyeId, vehicleLengthId, transportLicense, drivingLicensePic, drivingLicenseGuaPic);
            Result.ok(null, state?InfoCode.SUCCESS:InfoCode.FAILURE, response);
        } catch (SQLDataException e) {
            Result.ok(null, InfoCode.VEHICLE_EXIXTS_ERROR, response);
        }
        return null;
    }

    @RequestMapping(value = "vehicle/update", method = RequestMethod.POST)
    public String updateVehicle(@CookieValue(required = true,value = "driverToken") String driverToken,
                             @RequestParam(required = false) String plateNumber,
                             @RequestParam(required = false) Integer vehicleTpyeId,
                             @RequestParam(required = false) Integer vehicleLengthId,
                             @RequestParam(required = false) String transportLicense,
                             @RequestParam(required = false) String drivingLicensePic,
                             @RequestParam(required = false) String drivingLicenseGuaPic,
                             HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("司机---修改车辆信息---vehicle/update");
        request.setCharacterEncoding("UTF-8");
        Boolean state= driverService.updateVehicle(driverToken, plateNumber, vehicleTpyeId, vehicleLengthId,
                transportLicense, drivingLicensePic, drivingLicenseGuaPic,0,null,null);
        Result.ok(null, state?InfoCode.SUCCESS:InfoCode.FAILURE, response);
        return null;
    }

    @RequestMapping(value = "vehicle/delete", method = RequestMethod.POST)
    public String deleteVehicle(@CookieValue(required = false,value = "driverToken") String driverToken,
                             @RequestParam(required = false) Integer vehicleId,
                             HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("司机---删除车辆---vehicle/delete");
        request.setCharacterEncoding("UTF-8");
        Boolean state= driverService.deleteVehicle(driverToken,vehicleId);
        Result.ok(null, state?InfoCode.SUCCESS:InfoCode.FAILURE, response);
        return null;
    }

    @RequestMapping(value = "vehicle/list", method = RequestMethod.POST)
    public String vehicleList(@CookieValue(required = false,value = "driverToken") String driverToken,
                                HttpServletRequest request, HttpServletResponse response) throws IOException {
        logger.info("司机---车辆列表---vehicle/list");
        request.setCharacterEncoding("UTF-8");
        try {
            Result.ok(driverService.getVehicleList(driverToken), InfoCode.SUCCESS, response);
        } catch (Exception e) {
            e.printStackTrace();
            Result.ok(null, InfoCode.FAILURE, response);
        }
        return null;
    }



}
